<?php
/**
 * @since 27.08.2011
 * @author Akulov Denis <akulov.d.g@gmail.com>
 */
class MVC_View_Hook_Element implements MVC_View_Hook_Element_Interface {

    /**
     * @var Config_Interface
     */
    private $_objConfig = null;


    private $_strHookName = '';


    private $_strHookFile = '';


    private $_arVariables = array();


    public function __construct ($strHookName, $strHookFile, Config_Interface $objConfig) {
        $this->_strHookName = $strHookName;
        $this->_strHookFile = $strHookFile;
        $this->_objConfig   = $objConfig;
    }


    public function _addVariable ($strName, $mixValue) {
        $this->_arVariables[$strName] = $mixValue;
    }


    public function renderAsInsertHook ($strContent) {
        return $this->_render(
            '<!--insert:'.$this->_strHookName.' -->',
            $strContent
        );
    }


    public function renderAsAddHook ($strContent) {
        return $this->_render(
            '<!--add:'.$this->_strHookName.' -->',
            $strContent
        );
    }


    private function _render ($strHookTemplate, $strContent) {
        $strHookContent = $this->_getHookContent().$strHookTemplate;
        return str_replace($strHookTemplate, $strHookContent, $strContent);
    }


    private function _getHookContent () {
        $strPathToHook = $this->_objConfig->getAttribute('FW_PATHS', 'PROTOTYPE')
                       . 'MVC/V/Hooks/'
                       . str_replace('_', '/', $this->_strHookFile).'.php';

        foreach ($this->_arVariables as $name => $value) {
            $$name = $value;
        }

        $strResult = '';
        ob_start();

        require $strPathToHook;

        $strResult = ob_get_contents();
        ob_end_clean();

        return $strResult;
    }

}